package org.ihelpuoo.weboo;

import java.util.Calendar;
import java.util.Iterator;
import java.util.Locale;

import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.ihelpuoo.lang.SegmentNotFoundException;
import org.ihelpuoo.parser.segment.Segment;
import org.ihelpuoo.weboo.exception.ModuleNotFoundException;
import org.ihelpuoo.weboo.module.AbstractContext;
import org.ihelpuoo.weboo.module.ModuleContext;

public class WebooUtils
{
	private static final Category	log	= Logger.getLogger(WebooUtils.class);

	public static Segment formatSegment(ModuleContext context, Locale locale, Segment seg)
	{
		String contextPath = context.getWebooConfig().getContextPath();
		Iterator<String> it = seg.iterator();

		while (it.hasNext())
		{
			String keyName = it.next();
			int pos = keyName.indexOf(':');
			if (-1 == pos)
				continue;

			try
			{
				String prefix = keyName.substring(0,pos);
				String value = keyName.substring(pos + 1);

				int mnp = value.indexOf(':');
				ModuleContext myContext = context;
				if (mnp > 0)
				{
					String mn = value.substring(0,mnp);
					value = value.substring(mnp + 1);
					if (!myContext.getModule().getName().equals(mn))
						myContext = context.getModuleManager().getModule(mn).getModuleContext();
				}
				if (prefix.equals("msg"))
				{
					seg.set(keyName,myContext.getMessage(locale,value));
				}
				else if (prefix.equals("icon"))
				{
					if (value.lastIndexOf('.') == -1)
						value += Weboo.DEF_ICON_EXTENSION;
					seg.set(keyName,contextPath + myContext.getStyleHome() + "/icon/" + value);
				}
				else if (prefix.equals("horn"))
				{
					if (value.lastIndexOf('.') == -1)
						value += Weboo.DEF_ICON_EXTENSION;
					seg.set(keyName,contextPath + myContext.getStyleHome() + "/horn/" + value);
				}
				else if (prefix.equals("btn"))
				{
					if (value.lastIndexOf('.') == -1)
						value += Weboo.DEF_ICON_EXTENSION;
					seg.set(keyName,contextPath + myContext.getStyleHome() + "/btn/" + value);
				}
				else if (prefix.equals("pic"))
				{
					if (value.lastIndexOf('.') == -1)
						value += Weboo.DEF_PIC_EXTENSION;
					seg.set(keyName,contextPath + myContext.getStyleHome() + "/pic/" + value);
				}
				else if (prefix.equals("url"))
				{
					seg.set(keyName,contextPath + "/" + myContext.getModule().getName() + "/" + value);
				}
				else
				{
					seg.set(keyName,keyName);
				}
			}
			catch (ModuleNotFoundException e)
			{
				log.warn(e.getMessage());
			}
		}
		seg.set("time",Calendar.getInstance().getTimeInMillis());
		return seg;
	}

	public static Segment formatSegment(AbstractContext context, Segment seg)
			throws SegmentNotFoundException
	{
		return formatSegment(context.getModuleContext(),context.getSessionLocale(),seg);
	}

	public static int getModuleNameFromPath(String path, StringBuffer sb)
	{
		int i = 1;
		for (; i < path.length(); i++)
		{
			char c = path.charAt(i);
			if ('/' == c)
				break;
			sb.append(c);
		}
		return i;
	}

}
